//
//  GoogleViewModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/6.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class GoogleViewModel: BaseViewModel {
    @objc dynamic  var refreshBool = false //监听的状态
    
    open var model:SecurityModel? //传递的model
    
    
    //传递支持币种
    lazy var coninKidArray:Array<TradeCoinModel> = {
        let array:Array<TradeCoinModel> = []
        return array
    }()
    
    lazy var coinNameArr = [String]()
    var receiveModel:ReceiveCoinModel = ReceiveCoinModel() //币币地址model
    
    var selectModel:TradeCoinModel = TradeCoinModel()
    
    lazy var codeRequest:GoogleRequest = {
        let api = GoogleRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    lazy var rechargeRequest:ReceiveRequest = {
        let api = ReceiveRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    var googleModel:GoogleModel?
    
    var googleBool:Bool = false //标记谷歌认证 true / 接收比特币
    
    func archiveSelectModel(selectIndex:Int) {
        if coninKidArray.count > selectIndex {
            selectModel = coninKidArray[selectIndex]
        }
    }
    
    func archiveCoinInformation() {
        self.request(request: rechargeRequest, success: {[weak self] (result) in
            if let data = result?["data"] as? [String:Any] {
                dPrint(data)
                if let jsonData = data.jsonData {
                    do{
                        self?.receiveModel = try JSONDecoder().decode(ReceiveCoinModel.self, from: jsonData)
                        self?.refreshBool = true
                    }catch {
                        dPrint(error)
                    }
                }
            }
        }) { (response) in
            
        }
    }
    
    func archiveSlectName() {
        for model in coninKidArray {
            coinNameArr.append(model.coinCode != nil ? model.coinCode! : "")
        }
    }
    
    
    /// 获取谷歌二维码 地址
    ///
    /// - Parameters:
    ///   - success: 成功回调
    ///   - fail: 错误回调
    func archiveGoogleInformation(success:@escaping success, fail:@escaping fail) {
        self.request(request: codeRequest, success: success, fail: fail)
    }
}
extension GoogleViewModel:RequestCallBack,RequestDataReformer,RequestParamSource {
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if successBlock != nil {
            successBlock!(data)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if failBlock != nil {
            failBlock!(request)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? ReceiveRequest {
            pramas.updateValue(selectModel.coinCode != nil ? selectModel.coinCode! : "", forKey: "coinType")
            pramas.updateValue(AppInfo.default.userId != nil ? AppInfo.default.userId! : "", forKey: "userId")
        }
        
        return pramas
    }
    
    
}
